/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.ruchin.tek.employees.json;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;
import java.util.Collection;
import ru.ruchin.tek.employees.data.Department;

/**
 * @author yruchin
 */
public class DepartmentGsonSerializer implements JsonSerializer<Department> {
    @Override
    public JsonElement serialize(Department d, Type type, JsonSerializationContext jsc) {
        JsonObject jo = new JsonObject();

        jo.addProperty("isDepartment", Boolean.TRUE);
        jo.addProperty("id", d.getId());
        jo.addProperty("displayName", d.getName());
        
        JsonElement je = jsc.serialize(d.getEmployees(), Collection.class);
        jo.add("children", je);
        
        return jo;
    }
}
